package com.pay.ioopos.fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.fragment.app.Fragment;

import com.pay.ioopos.ErrorListener;
import com.pay.ioopos.KeyCodeFactory;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.R;


public class ShopStaffIdFragment extends Fragment implements View.OnKeyListener {
    private EditText staffIdView;
    private OnStaffIdListner listner;

    public ShopStaffIdFragment(OnStaffIdListner listner) {
        this.listner = listner;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop_staff_id, container, false);
        staffIdView = view.findViewById(R.id.input_staff_id);
        staffIdView.setOnKeyListener(this);
        staffIdView.requestFocus();
        return view;

    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_DOWN) {
            return false;
        }
        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(event.getKeyCode());
        if (null == keyInfo ) {
            return false;
        }
        if (keyInfo == KeyInfo.KEY_CANCEL) {
            listner.onStaffIdCancel();
        } if (keyInfo == KeyInfo.KEY_DELETE) {
            staffIdView.setText("");
        } else if (keyInfo == KeyInfo.KEY_ENTER && staffIdView.getText().toString().trim().length() > 0) {
            listner.onStaffIdFinish(staffIdView.getText().toString());
        }
        return false;
    }

    public interface OnStaffIdListner extends ErrorListener {
        void onStaffIdFinish(String staffId);
        void onStaffIdCancel();
    }

}
